<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">

      <el-input @keyup.enter.native="handleFilter" v-model="listQuery.serviceName" style="width: 200px;" class="filter-item" placeholder="维修人员名">
      </el-input>

      <el-select clearable style="width:120px" class="filter-item" v-model="listQuery.category" placeholder="类别">
        <el-option v-for="item in categoryData" :key="item.code" :label="item.name" :value="item.code">
        </el-option>
      </el-select>

      <el-select clearable style="width:120px" class="filter-item" v-model="listQuery.terminalBuilding" @change="selectRegion" placeholder="航站楼">
        <el-option v-for="item in terminalBuildingData" :key="item.regionCode" :label="item.regionName" :value="item.regionCode">
        </el-option>
      </el-select>

      <el-select clearable style="width:120px" class="filter-item" v-model="listQuery.region" @change="selectPosition" placeholder="区域">
        <el-option v-for="item in regionData" :key="item.regionCode" :label="item.regionName" :value="item.regionCode">
        </el-option>
      </el-select>

      <el-select clearable style="width:120px" class="filter-item" v-model="listQuery.position" placeholder="位置">
        <el-option v-for="item in positionData" :key="item.regionCode" :label="item.regionName" :value="item.regionCode">
        </el-option>
      </el-select>

      <el-date-picker style="width: 200px;" class="filter-item" v-model="listQuery.startDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始时间">
      </el-date-picker>

      <el-date-picker style="width: 200px;" class="filter-item" v-model="listQuery.endDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束时间">
      </el-date-picker>

      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>

      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleDownload">导出</el-button>

    </div>

    <el-table :key='tableKey' id="mainte" :data="list" v-loading="listLoading" element-loading-text="给我一点时间" stripe border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="报修人员">
        <template slot-scope="scope">
          <span>
           {{scope.row.repairName}}
          </span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="报修时间">
        <template slot-scope="scope">
          <span>
           {{scope.row.repairTime}}
          </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="设备名称">
        <template slot-scope="scope">
          <span>
           {{scope.row.facilityName}}
          </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="航站楼">
        <template slot-scope="scope">
          <span>{{scope.row.terminalBuildingName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="区域">
        <template slot-scope="scope">
          <span>{{scope.row.regionName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="位置">
        <template slot-scope="scope">
          <span>{{scope.row.positionName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="类别">
        <template slot-scope="scope">
          <span>{{scope.row.categoryName}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" class-name="status-col" label="状态">
        <template slot-scope="scope">
          <el-tag>{{scope.row.status | statusFilter}}</el-tag>
        </template>
      </el-table-column>

       <el-table-column align="center" label="设备问题">
        <template slot-scope="scope">
          <span>{{scope.row.problem}}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="报修备注">
        <template slot-scope="scope">
          <span>
           {{scope.row.repairComment}}
          </span>
        </template>
      </el-table-column>

       <el-table-column align="center" label="维修人员">
        <template slot-scope="scope">
          <span>{{scope.row.serviceName}}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="接单时间">
        <template slot-scope="scope">
          <span>
           {{scope.row.ordersTime}}
          </span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="维修方式">
        <template slot-scope="scope">
          <span>
           {{scope.row.processType}}
          </span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="维修结果">
        <template slot-scope="scope">
          <span>
           {{scope.row.processResult}}
          </span>
        </template>
      </el-table-column>

      <el-table-column v-if="isShow" align="center" label="维修完成时间">
        <template slot-scope="scope">
          <span>
           {{scope.row.serviceCompleteTime}}
          </span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleInfo(scope.row)">详情
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 详情   -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogDetails">
      <el-form :model="detailsData" ref="detailsData" label-width="120px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="报修人员：" prop="repairName">
              {{detailsData.repairName}}
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报修时间：" prop="repairTime">
              {{detailsData.repairTime}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名：" prop="facilityName">
              {{detailsData.facilityName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="航站楼：" prop="terminalBuildingName">
              {{detailsData.terminalBuildingName}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="区域：" prop="regionName">
              {{detailsData.regionName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置：" prop="positionName">
              {{detailsData.positionName}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="类别：" prop="categoryName">
              {{detailsData.categoryName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备问题：" prop="problem">
              {{detailsData.problem}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="报修备注：" prop="repairComment">
              {{detailsData.repairComment}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维修人员：" prop="serviceName">
              {{detailsData.serviceName}}
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="接单时间：" prop="ordersTime">
              {{detailsData.ordersTime}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维修方式：" prop="processType">
              {{detailsData.processType}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维修结果：" prop="processResult">
              {{detailsData.processResult}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维修完成时间：" prop="serviceCompleteTime">
              {{detailsData.serviceCompleteTime}}
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>维修前</span>
              </div>
              <div class="upload-container">
                <div class="component-item">
                  <div class="image-preview image-app-preview">
                    <div class="image-preview-wrapper" v-show="detailsData.evidenceImage != ''">
                      <img :src="detailsData.evidenceImage">
                      <div class="image-preview-action">
                        <i @click="rmImage"></i>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="12" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>维修后</span>
              </div>
              <div class="upload-container">
                <div class="component-item">
                  <div class="image-preview image-app-preview">
                    <div class="image-preview-wrapper" v-show="detailsData.processImage != ''">
                      <img :src="detailsData.processImage">
                      <div class="image-preview-action">
                        <i @click="rmImage"></i>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
@import 'src/styles/mixin.scss';
.upload-container {
  width: 100%;
  position: relative;
  @include clearfix;
  .image-uploader {
    width: 35%;
    float: left;
  }
  .image-preview {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .image-app-preview {
    width: 320px;
    height: 180px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
}
</style>

<script>
import {
  fetchList,
  getObj,
  getRegionByCode,
  getRegionByPid,
  allCategory
} from '@/api/maintenance/maintenance'
import waves from '@/directive/waves/index.js' // 水波纹指令
// import { parseTime } from '@/utils'
import { mapGetters } from 'vuex'
// import FileSaver from 'file-saver'
// import XLSX from 'xlsx'

export default {
  name: 'table_maintenance',
  directives: {
    waves
  },
  data() {
    return {
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        serviceName: '',
        startDate: '',
        endDate: '',
        terminalBuilding: '',
        region: '',
        position: '',
        category: ''
      },
      dialogFormVisible: false,
      dialogDetails: false,
      dialogStatus: '',
      terminalBuildingData: [],
      regionData: [],
      positionData: [],
      categoryData: [],
      isShow: false,
      detailsData: {
        repairName: '',
        repairTime: '',
        facilityName: '',
        problem: '',
        evidenceImage: '',
        repairComment: '',
        serviceName: '',
        ordersTime: '',
        processType: '',
        processImage: '',
        processResult: '',
        serviceCompleteTime: '',
        terminalBuildingName: '',
        regionName: '',
        positionName: '',
        categoryName: ''
      },
      textMap: {
        update: '编辑',
        create: '新建',
        info: '详情'
      },
      tableKey: 0
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: '正常',
        1: '报修中',
        2: '维修中'
      }
      return statusMap[status]
    }
  },
  created() {
    this.getList()
    getRegionByPid(0).then(response => {
      this.terminalBuildingData = response.data
    })
    allCategory().then(response => {
      this.categoryData = response.data
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery.orderByField = ''
      this.listQuery.isAsc = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleInfo(row) {
      getObj(row.id).then(response => {
        this.dialogStatus = 'info'
        this.dialogDetails = true
        this.detailsData = response.data
      })
    },
    // exportExcel() {
    //   this.pagesize = this.total // 表格长度变长
    //   // console.info(this.pagesize)
    //   this.currentPage = 1
    //   this.$nextTick(function() {
    //     var wb = XLSX.utils.table_to_book(document.querySelector('#mainte'))
    //     var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'buffer' })
    //     try {
    //       FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '维修记录.xlsx')
    //     } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
    //     this.pagesize = 20
    //     return wbout
    //   })
    // },
    handleDownload() {
      const lq = this.listQuery
      lq.limit = this.total
      fetchList(lq).then(response => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = ['报修人员', '报修时间', '设备名称', '航站楼', '区域', '位置', '类别', '状态', '设备问题', '报修备注', '维修人员', '接单时间', '维修方式', '维修结果', '维修完成时间']
          const filterVal = ['repairName', 'repairTime', 'facilityName', 'terminalBuildingName', 'regionName', 'positionName', 'categoryName', 'status', 'problem', 'repairComment', 'serviceName', 'ordersTime', 'processType', 'processResult', 'serviceCompleteTime']
          const list = response.data.records
          for (const item of list) {
            switch (item.status) {
              case 0:
                item.status = '正常'
                break
              case 1:
                item.status = '报修中'
                break
              case 2:
                item.status = '维修中'
                break
            }
          }
          this.listLoading = false
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '维修记录')
        })
        this.listQuery.limit = 20
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    selectRegion(code) {
      if (code != null && code !== '') {
        getRegionByCode(code).then(response => {
          this.regionData = response.data
        })
      }
    },
    selectPosition(code) {
      if (code != null && code !== '') {
        getRegionByCode(code).then(response => {
          this.positionData = response.data
        })
      }
    },
    rmImage() {}
  }
}
</script>
